package fr.ups.jim.isi.tp1 ;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Ji MA (ji.ma@u-psud.fr)
 *
 */
public class MyDialogBox extends JFrame implements ActionListener {
	
	JLabel label1;
	JButton button1;
	JButton button2;
	String choice;
	
    public MyDialogBox() {
	    label1 = new JLabel();
	    button1 = new JButton();
	    button2 = new JButton();
        button1.addActionListener(this);
        button2.addActionListener(this);
	   	Container content = this.getContentPane() ;
	   	this.setResizable(false);
	   	this.setLocation(400, 300);
	   	
	   	// use GridBagLayout Manager to manage the layout of "widgets"
	   	content.setLayout(new GridBagLayout());
	   	GridBagConstraints c_label1 = new GridBagConstraints();
	   	Insets inset_label = new Insets(0,30,0,30);
	   	c_label1.gridwidth = GridBagConstraints.REMAINDER;  
	   	c_label1.ipady = 40;
	   	c_label1.insets = inset_label;
	   	content.add(label1, c_label1);
  	
	   	Dimension dimension_button = new Dimension(75,30);
	   	// define button1 constraints
     	button1.setPreferredSize(dimension_button);
     	GridBagConstraints c_button1 = new GridBagConstraints();
     	c_button1.gridwidth = GridBagConstraints.RELATIVE;
     	Insets inset_b1 = new Insets(0,30,10,10);
     	c_button1.insets = inset_b1;
     	// add the second "widget" : JButton 1
     	content.add(button1, c_button1);
     	// add action listener to JButton 1
     	button1.addActionListener(new ActionListener() {
     		public void actionPerformed(ActionEvent e){
     		}
     	});
     	
     	//define button2 constraints 
    	button2.setPreferredSize(dimension_button);
    	GridBagConstraints c_button2 = new GridBagConstraints();
    	c_button2.gridwidth = GridBagConstraints.REMAINDER;
    	Insets inset_b2 = new Insets(0,0,10,10);
    	c_button2.insets = inset_b2;
    	// add the third "widget" : JButton 2
    	content.add(button2, c_button2);
     	// add action listener to JButton 1
     	button2.addActionListener(new ActionListener() {
     		public void actionPerformed(ActionEvent e){
     		}
     	});
     	
    }

    public MyDialogBox(String title, String message, String b1lbl, String b2lbl) {
    	this() ;
    	configure(title,message,b1lbl,b2lbl) ;
    }

    // set user-defined box title, message, and button names
    public void configure (String title, String message, String b1lb1, String b2lb1) {
    	this.setTitle(title);
    	label1.setText(message);
    	button1.setText(b1lb1);
    	button2.setText(b2lb1);
    	pack();
    	setVisible(true);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent e) {
    	JButton button = (JButton)e.getSource() ;
 	   	if (button == button1) {
 	   		choice = button1.getText();
 	   		System.out.println(this.waitForUserChoice());
 	   		System.exit(0) ;	
 	   	}
 	   	else if (button == button2) {
 	   		choice = button2.getText();
 	   		System.out.println(this.waitForUserChoice());
	   		System.exit(0) ;
 	   	}
    }
    
    public String waitForUserChoice() {
    	return choice;
    }
    
    public static void showBox() {
	   MyDialogBox box = new MyDialogBox("HandmadeDialog",
									   "Vous avez fini le premier exercice...",
									   "Stop", "Encore") ;
    }

    public static void main(String args[]) {
    	MyDialogBox.showBox();
    }


}
